<?php
/**
* Support for Internalization (I18N). See function <i>I18N()</i> for more details.
*<p>Never use this! Use function I18N() instead! Load and store translations for the current language. </p>
*<p> Language files are stored in "Engine/i18n/translations_[lang].dat" files.</p>
*<p>The basic idea is that you call the <i>I18N()</i> function whenever you want to translate
*something. Just keep that something always in english and this class will return you the translation
*in the current language. Where is the catch? That you must, via the ManagementPanel, provide translations
*for all the strings for the current language.</p>
*@package Core
*@subpackage I18N
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*/
class CoreTranslations extends BaseClassSingleton
{
	private $translations = array();
	private $modified = false;
	private $lang = false;
	private $lang_file = "";
	/**
	* Construct the class and load translations reading the lang config variable.
	*/
	public function __construct()
	{
		parent::__construct();
		$lang = $this->lang = Core::GetConfig("lang");
		$this->lang_file = "Engine/i18n/translations_$lang.dat";
		if ( file_exists( $this->lang_file ) )
			$this->translations = unserialize( file_get_contents( $this->lang_file ) );
	}
	/**
	* On closing, just check if there are modified translations to save and save them.
	*/
	public function __destruct()
	{
		if ( $this->modified )
		{
	#		file_put_contents( $this->lang_file, serialize( $this->translations ) );
			$this->modified = false;
		}
	}
	/**
	* Given a string, return its translation, if any. Otherwise return the string.
	*/
	public function Translate( $str )
	{
		if ( isset( $this->translations[$str] ) )
			return $this->translations[$str];
		$this->modified = true;
		return $this->translations[$str] = $str;
	}
}
?>